const app = getApp()
const api = app.globalData.api

Page({
    data: {
        salesAmount: {
            lazyLoad: true
        },
        result: [],
        items: {},
        salesDateRange: '',
        deliveryDateRange: '',
    },
    async main() {
        await this.dataBoard()
        await this.getStatData()
        await this.getDeliveryRate()
        await this.getCostProfit()
        await this.getTopData()
    },
    async dataBoard() {
        let res = await api.curlGet('?cla=inventoryOther&fun=dataBoard')
        if (res.code === 1) {
            this.setData({
                result: res.data
            })
        } else {
            api.showToast(res.warn)
        }
    },
    onChange: async function (obj) {
        let type = obj.detail.name
        if (['staff', 'kehu', 'material'].includes(type)) {
            this.getTopData(type)
        }
    },
    // 获取统计数据
    getStatData: async function (params = {}) {
        let that = this
        let res = await api.curlPost('?cla=saleStatistics', params)
        if (res.code === 1) {
            that.setData({
                salesDateRange: res.data.dateRange
            })
            return api.wxCharts(that.selectComponent('#salesAmount'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    // 按期交货率
    getDeliveryRate: async function (params = {}) {
        let that = this
        let res = await api.curlPost('?cla=saleStatistics&fun=deliveryRate', params)
        if (res.code === 1) {
            that.setData({
                deliveryDateRange: res.data.dateRange
            })
            return api.wxCharts(that.selectComponent('#deliveryRate'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    // 成本与利润
    getCostProfit: async function (params = {}) {
        let that = this
        let res = await api.curlPost('?cla=saleStatistics&fun=costProfit', params)
        if (res.code === 1) {
            that.setData({
                costDateRange: res.data.dateRange
            })
            return api.wxCharts(that.selectComponent('#costProfit'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    getTopData: async function (type = 'kehu', params = {}) {
        let that = this
        let res = await api.curlPost('?cla=saleStatistics&fun=' + type, params)
        if (res.code === 1) {
            that.setData({
                items: res.data
            })
        }
    },
    onLoad: async function (options) {
        await this.main()
    },
    // 搜索
    onSearch: async function (e) {
        if (e.detail) {
            let that = this
            let res = await api.curlPost('?cla=saleStatistics&fun=orderToId', {orderNo: e.detail})
            if (res.code !== 1) {
                api.showToast(res.warn, 'none')
            } else {
                wx.navigateTo({
                    url: 'detail?id=' + res.data,
                })
            }
        }
    },
    // 扫码
    onScan: async function (e) {
        wx.scanCode({
            onlyFromCamera: true,
            success(res) {
                if (res.errMsg === "scanCode:ok") {
                    const string = res.result
                    let array = api.parseStr(string)
                    console.log(array)
                    if (['sale_order', 'saleOrder'].includes(array['source'])) {
                        wx.navigateTo({
                            url: 'detail?id=' + array['id'],
                        })
                    }
                }
            }
        })
    },
});
